<template>
  <basic-container>
    <el-form :inline="true" :model="query" class="demo-form-inline">
      <el-row>
        <el-form-item label="项目编号:">
          <el-input v-model="query.projectCode" clearable placeholder="项目编号" size="small"></el-input>
        </el-form-item>
        <el-form-item label="项目名称:">
          <el-input v-model="query.projectName" clearable placeholder="项目名称"></el-input>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item>
          <el-button size="small" type="primary" icon="el-icon-search" @click="onSearch" :loading="loading">搜索
          </el-button>
          <el-button size="small" icon="el-icon-delete" @click="searchReset" :loading="loading">清空</el-button>
        </el-form-item>
      </el-row>
    </el-form>
    <div class="btn-operate">
      <el-button v-if="permissions.servicecharge_add" @click="addChargeForm" type="primary" size="small" icon="el-icon-plus">
        新增
      </el-button>
    </div>
    <el-table v-loading="loading" :data="tableData" border
              highlight-current-row style="width: 100%">
      <el-table-column label="项目编号" prop="projectName" width="180px">
        <template slot-scope="scope">
          <el-table :data="scope.row.projectList" style="width: 100%" border :show-header="false">
            <el-table-column prop="projectCode" label="项目编号"></el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column label="项目名称" prop="projectName" width="200px">
        <template slot-scope="scope">
          <el-table :data="scope.row.projectList" style="width: 100%" border :show-header="false">
            <el-table-column prop="projectName" label="项目名称"></el-table-column>
          </el-table>
        </template>
      </el-table-column>
      <el-table-column label="合同名称" prop="contractName" width="200px"></el-table-column>
      <el-table-column label="批次" prop="batch" width="50px"></el-table-column>
      <el-table-column label="金额(元)" prop="totalAmount" width="100px">
        <template slot-scope="scope">
          {{scope.row.totalAmount | toThousandslsFilter}}
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status" width="150px">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status | statusTag">{{scope.row.status | status}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="支付凭证" prop="projectCode">
        <template #default="{ row, $index }">
          <el-upload
            class="upload-demo"
            :headers="{Authorization}"
            :on-preview="handlePreview"
            :show-file-list="true"
            :file-list="row.fileList">
          </el-upload>
        </template>
      </el-table-column>
      <el-table-column label="审核意见" prop="remark">
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
<!--          <el-button type="text" size="small" icon="el-icon-view" @click="showViewBox(scope.row)">查看</el-button>-->
          <el-button v-if="permissions.servicecharge_add && (scope.row.status === 0 || scope.row.status === 2)" type="text" size="small" icon="el-icon-edit" @click="showEditBox(scope.row)">编辑</el-button>
          <el-button v-if="permissions.servicecharge_confirm && scope.row.status === 1" type="text" size="small" icon="el-icon-edit" @click="showConfirmBox(scope.row)">审核</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="sizeChange"
        @current-change="currentChange"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 30, 40 ,50 ,100]"
        :page-size="10"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
    <el-dialog :append-to-body="true" :before-close="resetForm" :close-on-click-modal="false"
               title="服务费支付管理" :visible.sync="confirmBox" custom-class="ledger" fullscreen top="5vh"
               width="1200px">
      <el-form ref="dataForm" :model="form" class="el-dialog__body" label-width="100px">
        <div class="title">项目信息</div>
        <el-row>
          <el-col :span="8">
            <el-form-item label-width="100px" label="合同名称:" prop="projectName">
                {{form.contractName}}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label-width="100px" label="总金额:" prop="projectName">
              {{form.totalAmount | toThousandslsFilter}}
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label-width="100px" label="批次:" prop="projectName">
              {{form.batch}}
            </el-form-item>
          </el-col>
        </el-row>
        <div class="title">支付项目</div>
        <el-row>
          <el-col>
            <el-table
              ref="table"
              :data="tableData"
              :model="tableData"
              style="width: 50%;margin: auto"
              max-height="450"
              highlight-current-row
            >
              <el-table-column type="selection" width="55" align="center"></el-table-column>
              <el-table-column label="序号" width="100">
                <template #default="{ $index }">
                  <span>{{ $index + 1 }}</span>
                </template>
              </el-table-column>
              <el-table-column label="项目名称">
                <template #default="{ row }">
                  {{row.projectName}}
                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
        <div class="title">支付凭证</div>
        <el-row>
          <el-col>
            <el-table
              ref="table"
              :data="tableFileData"
              :model="tableFileData"
              v-loading="loading"
              style="width: 70%;margin: auto"
              max-height="450"
              highlight-current-row
            >
              <el-table-column label="序号" width="100">
                <template #default="{ $index }">
                  <span>{{ $index + 1 }}</span>
                </template>
              </el-table-column>
              <el-table-column label="名称">
                <template #default="{ row }">
                  <el-input v-model="row.name" type="textarea" :autosize="{ minRows: 1}"></el-input>
                </template>
              </el-table-column>
              <el-table-column label="附件">
                <template #default="{ row, $index }">
                  <el-upload
                    class="upload-demo"
                    :headers="{Authorization}"
                    :on-preview="handlePreview"
                    :show-file-list="true"
                    :file-list="row.fileList">
                  </el-upload>
                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
        <div class="title">审核备注</div>
        <el-row>
          <el-col>
            <el-form-item label-width="100px" label="合同名称:" prop="projectName">
              <el-input v-model="form.remark" placeholder="审核备注" type="textarea"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button icon="el-icon-close" size="small" @click="resetForm">关闭</el-button>
        <el-button icon="el-icon-check" size="small" type="primary" @click="rejectCharge" :loading="loading">驳回</el-button>
        <el-button icon="el-icon-check" size="small" type="primary" @click="confirmCharge" :loading="loading">确认</el-button>
      </span>
    </el-dialog>

    <table-form ref="reportForm" @cancelDialog="closeChargeFormDialog"></table-form>
  </basic-container>
</template>
<script>
import TableForm from "@/components/form/engineeringChargeForm.vue";
import {fetchList, rejectCharge, confirmCharge, getCharge} from "@/api/adis/engineering/charge";
import {mapGetters} from "vuex";
import AuditEngineeringReportDraftView from "@/components/actForm/AuditEngineeringReportDraftView";
import {toThousandslsFilter} from "@/filters";
import {rejectSubmit} from "@/api/adis/track/rectifyReprot";

export default {
  components: {AuditEngineeringReportDraftView, TableForm},
  computed:{
    ...mapGetters(["permissions"])
  },
  data(){
    return{
      query: {},
      form: {},
      tableData:[],
      newData:[],
      loading:false,
      processId : '',
      page: {
        size: 10,
        current: 1,
        total: 0
      },
      viewBox: false,
      Authorization:'',
      confirmBox:false,
      fileList:[],
      tableFileData:[]
    }
  },
  created() {
    this.onLoad(this.page);
    this.Authorization = 'Bearer ' + this.$store.getters.access_token
  },
  methods:{
    addChargeForm(row){
      this.$nextTick(() => {
        this.$refs.reportForm.init(row,true);
      })
    },
    closeChargeFormDialog(){
      this.onLoad(this.page);
    },
    onSearch(){
      this.page.current = 1;
      this.onLoad(this.page);
    },
    searchReset() {
      this.query = {};
      this.page.current = 1;
      this.onLoad(this.page)
    },
    showEditBox(row) {
      this.addChargeForm(row);
    },
    showConfirmBox(row){
      this.confirmBox = true;
      getCharge(row.chargeId).then(response => {
        this.form = response.data.data;
        this.$set(this, 'tableFileData', this.form.serviceChargeAttachmentDTOS)
        this.tableFileData.forEach((item, i) => {
          item.fileList = [];
          item.attachments.forEach(col => {
            item.fileList.push({name: col.fileName, url: col.fileUrl})
          })
        })
        this.tableData = this.form.projectList || [];
      })
    },
    resetForm(){
      this.confirmBox = false;
      this.form = {};
      this.tableData = [];
      this.tableFileData = [];
      this.onLoad(this.page);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      fetchList(Object.assign(params, this.query, page)).then(res => {
        this.loading = false;
        const data = res.data.data;
        this.page.total = data.total;
        this.tableData = data.records;
        this.tableData.forEach(item => {
          item.fileList = []
          item.attachments.forEach(col => {
            item.fileList.push({name: col.fileName, url: col.fileUrl})
          })
        })
      }, () => {
        this.loading = false;
      });
    },
    sizeChange(size) {
      this.page.size = size;
      this.onLoad(this.page);
    },
    currentChange(current) {
      this.page.current = current;
      this.onLoad(this.page);
    },
    rejectCharge(){
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.form.attachments = [];
          this.fileList.forEach(file => {
            this.form.attachments.push({fileName: file.name, fileUrl: file.url})
          })
          this.form.projectList = []
          this.tableData.forEach((item, i) => {
            this.form.projectList.push(item)
          })
          this.loading = true;
          rejectCharge(this.form).then(() => {
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.loading = false;
            this.resetForm();
          }, () => {
            this.loading = false;
          });
        } else {
          return false;
        }
      });
    },
    confirmCharge(){
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.form.attachments = [];
          this.fileList.forEach(file => {
            this.form.attachments.push({fileName: file.name, fileUrl: file.url})
          })
          this.form.projectList = []
          this.tableData.forEach((item, i) => {
            this.form.projectList.push(item)
          })
          this.loading = true;
          confirmCharge(this.form).then(() => {
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.loading = false;
            this.resetForm();
          }, () => {
            this.loading = false;
          });
        } else {
          return false;
        }
      });
    },

    handlePreview(file) {
      window.open(file.url ? file.url : file.response.data.url)
      // const a = document.createElement('a');
      // let url = file.url ? file.url : file.response.data.url;
      // console.log(url);      // let fileName = '你想要的文件名.docx'; // 这里设置你想要的文件名，包括扩展名
      // a.setAttribute('download', file.name);
      // a.setAttribute('href', url);
      // a.click();
    },
  }
}
</script>
<style scoped>
::v-deep .el-upload-list__item {
  transition: none !important;
}
::v-deep .el-upload-list__item-thumbnail {
  /* 图片在方框内显示长边 */
  object-fit: scale-down !important;
}
</style>
